
const apiHost = import.meta.env.VITE_API_HOST || 'https://docsapi.arc53.com';
// login.ts
async function login(email: string, password: string): Promise<string> {
    try {

      const apiHost = import.meta.env.VITE_API_HOST || '';
      const response = await fetch(apiHost+'/auth/login', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        //   "Authorization": `Bearer ${getToken()}`
        },
        body: JSON.stringify({ email, password }),
      });
  
      if (!response.ok) {
        throw new Error('Login failed');
      }
  
      const data = await response.json();
      // 将获取到的token存储在sessionStorage中
      sessionStorage.setItem('access_token', data.access_token); 
      return data.access_token;
    } catch (error) {
      console.error(error);
      throw error;
    }
  }
  
  // 提供一个获取sessionStorage中token的函数
  function getToken(): string | null {
    return sessionStorage.getItem('access_token');
  }
  
  export { login, getToken };